package upload

import (
	"blog/errmsg"
	"blog/utils"
	"context"
	"github.com/qiniu/api.v7/v7/auth/qbox"
	"github.com/qiniu/api.v7/v7/storage"
	"io"
)

func UploadFileToQiniu(file io.Reader,fileSize int64)(string,int){
	//生成token凭证
	putPolicy := storage.PutPolicy{
		Scope:utils.Bucket,
	}
	mac := qbox.NewMac(utils.AccessKey,utils.SecretKey)
	upToken := putPolicy.UploadToken(mac)

	putExtra := storage.PutExtra{}

	ret:=storage.PutRet{}

	cfg := storage.Config{
		Zone:&storage.ZoneHuanan,
		UseHTTPS:false,
		UseCdnDomains:false,
	}

	//form 上传对象
	formUploader := storage.NewFormUploader(&cfg)

	err := formUploader.PutWithoutKey(context.Background(),&ret,upToken,file,fileSize,&putExtra)
	if err!=nil{
		return err.Error(),errmsg.FAIL
	}
	url := utils.QiniuServer + ret.Key
	return url,errmsg.SUCCESS
}
